<?php
/**
*    Images Saler - Component
*    Author : hm_advert@ymail.com
*    
**/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.client.helper');
jimport('joomla.application.component.model');

class EcommerceModelUser extends JModel
{
	
	var $_id = null;
	var $_table = '#__users';
	var $_lists = '';
	var $_data = null;
		
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
		
	}

	function setId($id)
	{
		$this->_id		= $id;
	}
	
	
	function &getData()
	{
		if (empty( $this->_data )) 
		{
			$query = ' SELECT * FROM #__users '.
					'  WHERE id = '.$this->_id;
					
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();	
			
		}

		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->type = null;
			$this->_data->model = null;
			$this->_data->register_day = null;
			$this->_data->short_description= null;
			$this->_data->view_day = null;
			$this->_data->registry = null;
			$this->_data->stand = null;
			$this->_data->km = null;
			$this->_data->description = null;
			$this->_data->information1 = null;
			$this->_data->information2 = null;
			$this->_data->image = null;
			$this->_data->image1 = null;
			$this->_data->image2 = null;
			$this->_data->image3 = null;
			$this->_data->image4 = null;
			$this->_data->ordering = null;
			$this->_data->published = null;
			
		}
		return $this->_data;
	}
	
	

	function store()	{
		global $mainframe;
		$row =& $this->getTable();
		if(!$row->bind(JRequest::get('post'))){
			echo '<script>alert("'.$row->getError().'");window.history.go(-1);</script>\n';
			exit();
		}
		
		//all these lines are used to avoid lost data form post
		$row->name = JRequest::getVar('name');
		$row->email = JRequest::getVar('email');
		$row->company = JRequest::getVar('company');
		$row->cvr = JRequest::getVar('cvr');
		$row->address = JRequest::getVar('address');
		$row->code = JRequest::getVar('code');
		$row->city = JRequest::getVar('city');
		$row->phone = JRequest::getVar('phone');
		
		//print_r($row);exit();	
		if(!$row->store()){
			echo '<script>alert("'.$row->getError().'");window.history.go(-1);</script>';
			exit();
		}
		else
		{
			return true;
		}
	}

	function delete()
	{
		global $mainframe;
		$cid = JRequest::getVar('cid',array(),'','array');
		$db = JFactory::getDBO();
		if(count($cid)> 0 )
		{
			$cids = implode(',',$cid);						
			
			$query = "DELETE FROM #__users WHERE id IN ($cids)";
			$db->setQuery($query);
			if(!$db->query())
			{
				echo "<script> alert('".$db->getErrorMsg()."');window.history.go(-1); </script>\n";
			}
		}

		return true;
	}

	function publish($publish) {
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		JArrayHelper::toInteger($cid);
	
		if ( count( $cid ) < 1 ) {
			$action = $publish ? 'Publish' : 'Unpublish';
			JError::raiseError(500, JText::_( 'Select Item to '.$action, true ) );
		}
		
		$cids = implode( ',', $cid );
		$count = count( $cid );
		$query = 'UPDATE #__pr_product'
		. ' SET published = '.(int) $publish
		. ' WHERE id IN ( '.$cids.' )'
		;

		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg() );
		}
	
	}
	
	function bought($publish) {
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		JArrayHelper::toInteger($cid);
	
		if ( count( $cid ) < 1 ) {
			$action = $publish ? 'Bought' : 'Unbought';
			JError::raiseError(500, JText::_( 'Select Item to '.$action, true ) );
		}
		
		$cids = implode( ',', $cid );
		$count = count( $cid );
		$query = 'UPDATE #__pr_product'
		. ' SET bought = '.(int) $publish
		. ' WHERE id IN ( '.$cids.' )'
		;

		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg() );
		}
	
	}
	
	function orderSection( $uid, $inc)
	{
		$row =& $this->getTable();
		$row->load( $uid );
		$row->move( $inc);
	}

	function saveOrder( &$cid )
	{
		$row =& $this->getTable();
	
		$total		= count( $cid );
		$order		= JRequest::getVar( 'order', array(0), 'post', 'array' );
		JArrayHelper::toInteger($order, array(0));
	
		// update ordering values
		for( $i=0; $i < $total; $i++ )
		{
			$row->load( (int) $cid[$i] );
			if ($row->ordering != $order[$i]) {
				$row->ordering = $order[$i];
				if (!$row->store()) {
					JError::raiseError(500, $db->getErrorMsg() );
				}
			}
		}
	
		$row->reorder();
	}


}

?>